import React from "react"
import ReactDOM from "react-dom"
import axios from "axios"
require('es6-promise').polyfill()
import AJAX from "../../lib/ajax"
import {addErrorListener} from "../../lib/handleError"
require('./testlog.less')

class App extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            log_info: {}
        }
    }
    componentWillMount() {
        addErrorListener()()
    }
    componentDidMount() {
        AJAX.appendBaiduAnalysis()
        this.getUrl()
    }
    getUrl() {
        const request_url = `${AJAX.common}/api/gateway`
        let params = {
            full_url: window.location.href,
            service: "web.auth.get_auth_url",
            datastr: Date.parse(new Date())
        }
        const request_params = JSON.stringify(params)
        let log_info = {
            request_url: `1.request_url: ${request_url}`,
            request_params: `2.request_params: ${request_params}`
        }
        this.setState({
            log_info
        })
        axios.get(request_url, { params })
            .then(response => {
                log_info["response"] = `3.request_response_data: ${JSON.stringify(response.data)}`
                this.setState({
                    log_info,
                    auth_url: response.data.body.auth_url
                })
            }).catch(error => {
                log_info["error"] = `4.request_error: ${JSON.stringify(error)}`
                this.setState({
                    log_info
                })
            })
    }

    makeLog(obj = {}) {
        let log_list = []
        for (let key in obj) {
            log_list.push(
                <div className="weui-cell comment-textarea" key={key}>
                    <div className="weui-cells__title lux-log">
                        {`${obj[key]}`}</div>
                </div>
            )
        }
        return <div className="log">{log_list}</div>
    }
    makeProContent() {
        const { log_info } = this.state
        return (<div>
            <div className="weui-cell comment-textarea">
                <div className="weui-cells__title lux-log">页面加载成功,当前地址{window.location.href}</div>
            </div>
            <div className="weui-cell comment-textarea">
                <a href="javascript:;"
                   className="weui-btn weui-btn_primary"
                   onClick={this.getUrl.bind(this)}
                >1. 点击发送请求</a>
            </div>
            {this.makeLog(this.state.log_info)}
            {
                this.state.auth_url
                    ? <div className="weui-cell comment-textarea">
                        <a href={this.state.auth_url}
                            className="weui-btn weui-btn_primary"
                        >2. 点击跳转至微信授权页面</a>
                    </div>
                    : null
            }
        </div>)
    }
    render() {
        let content = this.makeProContent()
        // asdasd
        return content
    }
}
ReactDOM.render(<App />, document.getElementById('app'))
if (module.hot) {
    module.hot.accept();
}